import {Component, OnInit} from "@angular/core";
import {Page} from "../../../public/util/page";
import {StarService} from "../star.service";
import {States} from "../../../public/setting/enums";
import {SettingUrl} from "../../../public/setting/setting_url";
import {Setting} from '../../../public/setting/setting';

@Component({
  selector: 'app-star-level-rule-list',
  templateUrl: './star-level-rule-list.component.html',
  styleUrls: ['./star-level-rule-list.component.scss']
})
export class StarLevelRuleListComponent implements OnInit {
  public searchParams: any = {};//搜索参数
  public starLevelRuleList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public states = States;       // 状态
  public msgTip = Setting.msgTip;

  constructor(private starService: StarService) {
  }

  ngOnInit() {
    this.queryStarLevelRuleList()
  }

  /**
   * 查询starLevelRule列表
   * @param curPage 当前页
   */
  queryStarLevelRuleList(curPage?: number) {
    this._loading = true;
    if (curPage) this.starLevelRuleList.curPage = curPage;//当有页码时，查询该页数据
    this.starLevelRuleList.params = {
      curPage: this.starLevelRuleList.curPage, //目标页码
      pageSize: this.starLevelRuleList.pageSize, //每页条数
    };
    this.starService.getStarLevelRuleList(this.starLevelRuleList.params).then((res: Page) => {
      this._loading = false;
      this.starLevelRuleList = res;
    }).catch(err => {
      this._loading = false;
    })
  }

  /**
   * 修改禁用状态
   * @param code
   * @param event:boolean
   */
  modifyStarLevelRuleState(code, event) {
    let state = event ? States.enable : States.disable;//转换为对应的枚举
    this.starService.modifyStarLevelRuleState(code, state).catch((res) => {
      this.queryStarLevelRuleList();//由于switch的特殊性，因此在失败的时候刷新页面
    })
  }

  /**
   * 重置搜索
   */
  resetSearch() {
    this.searchParams = {};
    this.queryStarLevelRuleList(1)
  }
}
